import allure
from selenium.webdriver.common.by import By

from base.base_page import BasePage
from testcases.user_center.conftest import delete_user, delete_code
from utils.assert_util import assertCompare
from utils.read import read_yaml


class UserPage(BasePage):

    #请登录
    please_login=(By.CSS_SELECTOR,'a[href="#/app/login"]')
    #会员中心
    vip_center = (By.XPATH, '//a[contains(text(),"会员中心")]')
    #我的订单
    my_order = (By.CSS_SELECTOR, 'a[href="#/app/home/member/order"]')
    #我的收藏
    my_collection = (By.CSS_SELECTOR, 'a[href="#/app/home/member/collection"]')
    #我的收货地址
    my_receive = (By.CSS_SELECTOR, 'a[href="#/app/home/member/receive"]')
    #购物车
    cart = (By.CSS_SELECTOR, 'a[href="#/app/shoppingcart/cart"]')

    """注册"""
    #点击免费注册
    register=(By.CSS_SELECTOR,'a[href="#/app/register"]')
    #输入框手机号
    input_mobile=(By.ID,'jsRegMobile')
    #获取验证码
    send_code = (By.ID, 'jsSendCode')
    #输入框验证码
    input_code = (By.ID, 'jsPhoneRegCaptcha')
    #输入框密码
    input_password = (By.ID, 'jsPhoneRegPwd')
    #注册按钮
    register_btn = (By.ID, 'jsMobileRegBtn')
    #退出
    exists=(By.XPATH,'//*[@id="ECS_MEMBERZONE"]/a[2]')
    #用户名
    user_name=(By.CSS_SELECTOR,'a[href="#/app/home/member/userinfo"]')
    #验证码错误
    code_failed=(By.CSS_SELECTOR,'#mobile_register_form > p:nth-child(5)')
    #用户已存在
    user_exist=(By.CSS_SELECTOR,'#mobile_register_form > p:nth-child(3)')

    """登录功能"""
    #用户名
    login_account=(By.ID,'account_l')
    # 密码
    login_password = (By.ID, 'password_l')
    # 登录按钮
    login_button = (By.ID, 'jsLoginBtn')
    # 登录用户名为空提示信息
    login_user_name = (By.XPATH, '//*[@id="jsLoginForm"]/p[1]')
    # 登录密码为空提示信息
    login_password_null = (By.XPATH, '//*[@id="jsLoginForm"]/p[2]')
    # 登录密码错误提示信息
    login_password_err = (By.XPATH, '//*[@id="jsLoginForm"]/p[3]')

    """收货地址"""
    # 省
    provice = (By.XPATH, '//td[contains(text(),"配送区域")]/following-sibling::td/div/div/select[1]')
    # 市
    city = (By.XPATH, '//td[contains(text(),"配送区域")]/following-sibling::td/div/div/select[2]')
    # 区
    area = (By.XPATH, '//td[contains(text(),"配送区域")]/following-sibling::td/div/div/select[3]')
    # 收货人姓名
    username = (By.ID, 'consignee_0')
    # 详细地址
    address = (By.ID, 'address_0')
    # 手机
    mobile = (By.ID, 'mobile_0')
    # 新增收货地址
    address_btn = (By.XPATH, '//button[contains(text(),"新增收货地址")]')
    # 收货地址确定修改按钮
    address_update_btn = (By.XPATH, '//button[contains(text(),"确定修改")]')
    # 配送区域
    peisong_area = (By.XPATH, '//td[contains(text(),"配送区域")]')
    #删除
    delete_address=(By.XPATH,'//button[contains(text(),"删除")]')

    def register_failed(self,data):

        with allure.step("点击免费注册"):
            self.click(self.register)
        with allure.step("输入手机号"):
            self.send_keys(self.input_mobile,data["mobile"])
        with allure.step("点击获取验证码"):
            self.click(self.send_code)
        with allure.step("输入验证码"):
            self.send_keys(self.input_code, "1234")
        with allure.step("输入密码"):
            self.send_keys(self.input_password, data["password"])
        with allure.step("点击注册并登录"):
            self.click(self.register_btn)
        with allure.step("获取验证码错误提示文本"):
            text = self.get_text(self.code_failed)
        assertCompare("验证码错误", "==", text)
        delete_user(data['mobile'])
        delete_code(data['mobile'])


    def login(self):
        self.get_url("http://meikefresh.5istudy.online/#/app/login")
        self.send_keys(self.login_account, read_yaml()["register_failed"]["mobile"])
        self.send_keys(self.login_password, read_yaml()["register_failed"]["password"])
        self.click(self.login_button)